﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Computer_Manager.DTO;

namespace Computer_Manager.DAL
{
    class DAL_order_detail
    {
        DbProvider db = new DbProvider();
        SqlCommand cmd;

        public int order_detail_insert(DTO_order_detail dt)
        {
            db.Open();
            int ID = 0;
            cmd = new SqlCommand("order_detail_Insert", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter thamso = new SqlParameter("@ID", ID);
            thamso.Direction = ParameterDirection.Output;
            cmd.Parameters.Add(thamso);
            cmd.Parameters.Add("@id_order", SqlDbType.Int).Value = dt.id_order;
            cmd.Parameters.Add("@id_product", SqlDbType.Int).Value = dt.id_product;
            cmd.Parameters.Add("@amount", SqlDbType.Int).Value = dt.amount;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            return (int)thamso.Value;
        }
        public void order_detail_Update(DTO_order_detail dt)
        {
            db.Open();
            cmd = new SqlCommand("order_detail_Update", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_detail_order", SqlDbType.Int).Value = dt.id_detail_order;
            cmd.Parameters.Add("@id_order", SqlDbType.Int).Value = dt.id_order;
            cmd.Parameters.Add("@id_product", SqlDbType.Int).Value = dt.id_product;
            cmd.Parameters.Add("@amount", SqlDbType.Int).Value = dt.amount;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        public void order_detail_Delete(DTO_order_detail dt)
        {
            db.Open();
            cmd = new SqlCommand("order_detail_Delete", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_detail_order", SqlDbType.Int).Value = dt.id_detail_order;
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            db.getConn().Close();
        }
        public DataTable GetAll()
        {
            SqlDataAdapter adapter = new SqlDataAdapter("order_detail_SelectAll", db.getConn());
            DataTable table = new DataTable();
            adapter.Fill(table);
            adapter.Dispose();
            return table;
        }
        public DataTable GetByID(DTO_order_detail dt)
        {
            cmd = new SqlCommand("order_detail_GetByID", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_detail_order", SqlDbType.Int).Value = dt.id_detail_order;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }

        public DataTable GetByOrderID(DTO_order_detail dt)
        {
            cmd = new SqlCommand("order_detail_GetByOrderID", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_order", SqlDbType.Int).Value = dt.id_order;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }


        public DataTable GetByProductId(DTO_order_detail dt)
        {
            cmd = new SqlCommand("order_detail_GetByProductId", db.getConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id_product", SqlDbType.Int).Value = dt.id_product;
            SqlDataAdapter adapter = new SqlDataAdapter(cmd);
            DataTable table = new DataTable();
            adapter.Fill(table);
            cmd.Dispose();
            adapter.Dispose();
            return table;
        }
    }
}
